
package view;

import controller.SessaoController;
import javax.swing.JOptionPane;
import model.Sessao;
import model.SessoesModel;

/**
 * @author Lucas Amaral
 */
public class SessaoView extends javax.swing.JFrame {
    private SessaoController controller;
    private SessoesModel model;
    private Sessao sessãoEditar;
    
    public SessaoView(SessoesModel m, Sessao sessão) {
        initComponents();
        model = m;
        controller = new SessaoController(this, model);
        controller.listarSalas();
        controller.listarFilmes(true);
        if (sessão != null) {
            jButtonSalvar.setVisible(false);
            sessãoEditar = sessão;
            jComboBoxFilme.setSelectedItem(sessão.getFilme().getNome());
            jComboBoxSala.setSelectedItem(sessão.getSala().getNumero());
            controller.quantidadeIngressosPossiveis(sessão.getSala().getId());
            jComboBoxQtdIngressosVendidos.setSelectedItem(sessão.getQtdIngressosVendidos());
            jComboBoxHora.setSelectedItem(String.valueOf(sessão.getData().getHours()));
            jComboBoxMinutos.setSelectedItem(String.valueOf(sessão.getData().getMinutes()));
            jDateChooserData.setDate(sessão.getData());
        } else  {
            jButtonSalvarAlterações.setVisible(false);
            jLabelQtdIngressosVendidos.setVisible(false);
            jComboBoxQtdIngressosVendidos.setVisible(false);
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jCalendar1 = new com.toedter.calendar.JCalendar();
        dateChooserPanelBeanInfo1 = new com.toedter.calendar.demo.DateChooserPanelBeanInfo();
        jLabelCabecalho = new javax.swing.JLabel();
        jButtonSalvar = new javax.swing.JButton();
        jLabelSala = new javax.swing.JLabel();
        jComboBoxSala = new javax.swing.JComboBox();
        jLabelFilme = new javax.swing.JLabel();
        jLabelData = new javax.swing.JLabel();
        jLabelHorario = new javax.swing.JLabel();
        jComboBoxHora = new javax.swing.JComboBox();
        jLabelDoisPontos = new javax.swing.JLabel();
        jComboBoxMinutos = new javax.swing.JComboBox();
        jDateChooserData = new com.toedter.calendar.JDateChooser();
        jComboBoxFilme = new javax.swing.JComboBox();
        jButtonCancelar = new javax.swing.JButton();
        jLabelQtdIngressosVendidos = new javax.swing.JLabel();
        jComboBoxQtdIngressosVendidos = new javax.swing.JComboBox();
        jButtonSalvarAlterações = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabelCabecalho.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        jLabelCabecalho.setText("Cadastro nova sessão");

        jButtonSalvar.setText("Salvar");
        jButtonSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSalvarActionPerformed(evt);
            }
        });

        jLabelSala.setText("Sala");

        jComboBoxSala.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBoxSalaActionPerformed(evt);
            }
        });

        jLabelFilme.setText("Filme");

        jLabelData.setText("Data");

        jLabelHorario.setText("Horário");

        jComboBoxHora.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "00", " ", " " }));

        jLabelDoisPontos.setText(":");

        jComboBoxMinutos.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59" }));

        jButtonCancelar.setText("Cancelar");
        jButtonCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelarActionPerformed(evt);
            }
        });

        jLabelQtdIngressosVendidos.setText("Ingressos vendidos");

        jButtonSalvarAlterações.setText("Salvar alterações");
        jButtonSalvarAlterações.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSalvarAlteraçõesActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabelSala)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jComboBoxSala, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabelFilme)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jComboBoxFilme, javax.swing.GroupLayout.PREFERRED_SIZE, 255, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabelCabecalho)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabelData)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jDateChooserData, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jLabelHorario)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jComboBoxHora, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabelDoisPontos)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jComboBoxMinutos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabelQtdIngressosVendidos)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jComboBoxQtdIngressosVendidos, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButtonSalvar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonSalvarAlterações)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButtonCancelar)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabelCabecalho)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jComboBoxFilme, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabelFilme)))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jComboBoxSala, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabelSala)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jComboBoxMinutos)
                    .addComponent(jComboBoxHora)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabelDoisPontos)
                        .addComponent(jLabelHorario))
                    .addComponent(jDateChooserData, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabelData, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(8, 8, 8)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelQtdIngressosVendidos)
                    .addComponent(jComboBoxQtdIngressosVendidos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonSalvar)
                    .addComponent(jButtonCancelar)
                    .addComponent(jButtonSalvarAlterações))
                .addGap(0, 8, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSalvarActionPerformed
        controller.inserirSessao(jDateChooserData.getDate(), jComboBoxHora.getSelectedItem().toString(), jComboBoxMinutos.getSelectedItem().toString(), 
                jComboBoxFilme.getSelectedItem().toString(), "0", jComboBoxSala.getSelectedItem().toString());
        JOptionPane.showMessageDialog(this, "Sessão cadastrada com sucesso");
        dispose();
    }//GEN-LAST:event_jButtonSalvarActionPerformed

    private void jButtonCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelarActionPerformed
        dispose();
    }//GEN-LAST:event_jButtonCancelarActionPerformed

    private void jButtonSalvarAlteraçõesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSalvarAlteraçõesActionPerformed
        controller.alterarSessao(sessãoEditar, jDateChooserData.getDate(), jComboBoxHora.getSelectedItem().toString(), jComboBoxMinutos.getSelectedItem().toString(), 
                jComboBoxFilme.getSelectedItem().toString(), jComboBoxQtdIngressosVendidos.getSelectedItem().toString(), jComboBoxSala.getSelectedItem().toString());
        dispose();
    }//GEN-LAST:event_jButtonSalvarAlteraçõesActionPerformed

    private void jComboBoxSalaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxSalaActionPerformed
        controller.quantidadeIngressosPossiveis(Integer.valueOf(jComboBoxSala.getSelectedItem().toString()));
    }//GEN-LAST:event_jComboBoxSalaActionPerformed

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new SessaoView(null, null).setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private com.toedter.calendar.demo.DateChooserPanelBeanInfo dateChooserPanelBeanInfo1;
    private javax.swing.JButton jButtonCancelar;
    private javax.swing.JButton jButtonSalvar;
    private javax.swing.JButton jButtonSalvarAlterações;
    private com.toedter.calendar.JCalendar jCalendar1;
    public javax.swing.JComboBox jComboBoxFilme;
    private javax.swing.JComboBox jComboBoxHora;
    private javax.swing.JComboBox jComboBoxMinutos;
    public javax.swing.JComboBox jComboBoxQtdIngressosVendidos;
    public javax.swing.JComboBox jComboBoxSala;
    private com.toedter.calendar.JDateChooser jDateChooserData;
    private javax.swing.JLabel jLabelCabecalho;
    private javax.swing.JLabel jLabelData;
    private javax.swing.JLabel jLabelDoisPontos;
    private javax.swing.JLabel jLabelFilme;
    private javax.swing.JLabel jLabelHorario;
    private javax.swing.JLabel jLabelQtdIngressosVendidos;
    private javax.swing.JLabel jLabelSala;
    // End of variables declaration//GEN-END:variables
}
